package cn.lixu._02xhr;


import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;

/**
 * @author lixu
 * @data 2022/08/16 0016 17:46:01
 * @param: null
 * @return: null
 **/
public class Producter {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        //1.获取Producter对象
        DefaultMQProducer producer = new DefaultMQProducer("xhr_product_group");
        //2.设置nameserver地址值
        producer.setNamesrvAddr("localhost:9876");
        //3.启动
        producer.start();
        //4.创建消息对象，设置消息
        // topic：消息主题 tags：此主题下的二级分类 body：消息的字节
        Message message = new Message("hello_topic", "hello", "清澈的爱，只为中国".getBytes(StandardCharsets.UTF_8));
        // 4.2.发送消息
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("成功消费了消息" + sendResult.toString());
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("消息消费失败" + throwable.toString());
            }
        });
        System.out.println("异步请求发送成功，请等待..........");

    }
}
